package jEDF.Montage;

import java.awt.event.*;

import javax.swing.*;

import jEDF.JEDF.*;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version : 2.0</p>
 */

public class MontageToolbar extends JToolBar {
    private Polysomnograph polysomnograph = null;

    private JButton jbNewMontage = new JButton("New");
    private JToggleButton jbPresetMontage = new JToggleButton("Preset");
    private JToggleButton jbMontageNone = new JToggleButton("None");
    private JToggleButton jbM1 = new JToggleButton("M1");
    private JToggleButton jbM2 = new JToggleButton("M2");
    private JToggleButton jbM3 = new JToggleButton("M3");
    private JToggleButton jbM4 = new JToggleButton("M4");

    private Montage[] montages = new Montage[4];

    private NewMontageDialog newMontageDialog = null;

    /**
     * @param mainWindow the main window
     * @param polysomnograph the polysomnograph where the montages are to be displayed
     */
    public MontageToolbar(JFrame mainWindow,
                          Polysomnograph polysomnograph) {
        this.polysomnograph = polysomnograph;

        newMontageDialog = new NewMontageDialog(mainWindow, this);

        for (int i = 0; i < montages.length; i++) {
            montages[i] = null;
        }

        add(jbNewMontage);
        add(jbMontageNone);
        add(jbPresetMontage);

        addSeparator();

        add(jbM1);
        add(jbM2);
        add(jbM3);
        add(jbM4);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jbMontageNone);
        buttonGroup.add(jbPresetMontage);
        buttonGroup.add(jbM1);
        buttonGroup.add(jbM2);
        buttonGroup.add(jbM3);
        buttonGroup.add(jbM4);

        jbMontageNone.setSelected(true);

        addListeners();
        updateTexts();
    }

    /**
     * should the user choose a different langage, this procedure will update
     * the display accordingly
     */
    public void updateTexts() {
        jbNewMontage.setText(JEDFMainWindow.getLocalizedString("New"));
        jbPresetMontage.setText(JEDFMainWindow.getLocalizedString("Preset"));
        jbMontageNone.setText(JEDFMainWindow.getLocalizedString("None"));
    }

    /* (non-Javadoc)
     * @see javax.swing.JComponent#setEnabled(boolean)
     */
    public void setEnabled(boolean bool) {
        jbNewMontage.setEnabled(bool);
        jbPresetMontage.setEnabled(bool && (Montage.isPresetAvailable()));
        jbM1.setEnabled(bool);
        jbM2.setEnabled(bool);
        jbM3.setEnabled(bool);
        jbM4.setEnabled(bool);
    }

    /**
     * this procedure is called when the user click on a M1..M4 button
     * to assign a new montage to this button
     */
    public void assignNewMontage() {
        newMontageDialog.assignNewMontage();
    }

    private void loadDefaultMontage() {
        polysomnograph.loadMontage(Montage.getDefaultMontage());
    }

    private void loadPresetMontage() {
        polysomnograph.loadMontage(Montage.getPresetMontage());
    }

    private void addListeners() {
        jbNewMontage.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                assignNewMontage();
            }
        });

        jbMontageNone.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                loadDefaultMontage();
            }
        });

        jbPresetMontage.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                loadPresetMontage();
            }
        });

        jbM1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                onButtonPress(0);
            }
        });

        jbM2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                onButtonPress(1);
            }
        });

        jbM3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                onButtonPress(2);
            }
        });

        jbM4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                onButtonPress(3);
            }
        });
    }

    private void onButtonPress(int numButton) {
        if (montages[numButton] == null) {
            montages[numButton] = newMontageDialog.getNewMontage();
            polysomnograph.loadMontage(montages[numButton]);
        }
        else {
            polysomnograph.loadMontage(montages[numButton]);
        }
    }

    /**
     * @param montage the montage to assign to a button
     * @param i the number of the button (0..3)
     */
    public void setMontage(Montage montage, int i) {
        montages[i] = montage;
        switch (i) {
            case 0:
                jbM1.setSelected(true);
                break;
            case 1:
                jbM2.setSelected(true);
                break;
            case 2:
                jbM3.setSelected(true);
                break;
            case 3:
                jbM4.setSelected(true);
                break;
        }
        polysomnograph.loadMontage(montages[i]);
    }

    /**
     * called to simulate a click on the "new montage" button
     */
    public void clickNewMontage() {
        jbNewMontage.doClick();
    }

    /**
     * called to simulate a click on the "none" button
     */
    public void clickMontageNone() {
        jbMontageNone.doClick();
    }

    /**
     * called to simulate a click on the "preset" button
     */
    public void clickPresetMontage() {
        jbPresetMontage.doClick();
    }

    /**
     * called to simulate a click on the "M1" button
     */
    public void clickM1() {
        jbM1.doClick();
    }

    /**
     * called to simulate a click on the "M2" button
     */
    public void clickM2() {
        jbM2.doClick();
    }

    /**
     * called to simulate a click on the "M3" button
     */
    public void clickM3() {
        jbM3.doClick();
    }

    /**
     * called to simulate a click on the "M4" button
     */
    public void clickM4() {
        jbM4.doClick();
    }

    /**
     * clear all the recorded montages
     */
    public void clear() {
        for (int i = 0; i < montages.length; i++) {
            montages[i] = null;
        }
        jbMontageNone.setSelected(true);
    }

}
